import React, { Component } from 'react';

import { inject, observer } from 'mobx-react';
import { List, Radio, Tools } from "weaver-mobile";
import { withRouter } from 'react-router-dom';
import { toJS } from 'mobx'
import * as EmSDK from '../../util/emSDK';

import { forwardViewForm } from '../../util/common';
import { RouteLayout } from 'weaver-mobile-page';
const ButtonGroup = RouteLayout.ButtonGroup;

const RadioItem = Radio.RadioItem;
const getLabel = Tools.getLabel;
@inject("wfworkflowAddStore")
@withRouter
@observer
export default class CreatorIdentity extends Component {
    componentWillMount() {
        const { wfworkflowAddStore } = this.props;
        const { setState, selectData } = wfworkflowAddStore;
        const { name = "", id = "" } = selectData;
        let parentURL = Tools.ss.getStr("__wf__openform__returnurl__")
        if (!id) window.location.href = parentURL;
        setState({ idSF: 0 })
        EmSDK.controlEmTopNav({ title: name, showCloseButton: false })
    }
    componentDidMount() {

    }
    componentWillUnmount() {
        EmSDK.controlEmTopNav({ title: getLabel(16392, "新建流程"), showCloseButton: false })
    }

    handleOnChange(d, type) {
        const { wfworkflowAddStore, } = this.props;
        const { setState, setResultDatas, selectData, idSF } = wfworkflowAddStore;
        if (idSF === d.id) {
            setState({ idSF: 0 })
        } else {
            setState({ idSF: d.id })
            if (type === "belongtoUsers") {
                setResultDatas({ isagent: "0", f_weaver_belongto_userid: d.id, beagenter: "0" })
            } else if (type === "beangener") {
                setResultDatas({ isagent: "1", f_weaver_belongto_userid: "0", beagenter: d.id })
            }
            setResultDatas({ iscreate: "1", workflowid: selectData.id })
        }

    }

    render() {
        const { selectData, idSF } = this.props.wfworkflowAddStore;
        let disabled = (idSF === 0);
        if (toJS(selectData)) {
            return (
                <div className="creatorIdentity" style={{ height: "100%" }}>
                    <div className="wf-add-creatorIdentity-listPart">
                        <div className="wf-add-creatorIdentity-list-title">{getLabel(84522, "选择创建身份")}</div>
                        <div>
                            {
                                toJS(selectData).belongtoUsers.length > 0 && <List renderHeader={() => getLabel(17747, "次账号")}>
                                    {toJS(selectData).belongtoUsers.map((d) => (
                                        <RadioItem
                                            key={d.id}
                                            checked={idSF === d.id}
                                            onClick={() => {
                                                this.handleOnChange(d, "belongtoUsers")
                                            }}
                                        >
                                            {d.subcompanyName || getLabel(391117, "未命名")}/{d.departmentName || getLabel(391117, "未命名")}/{d.jobtitlename || getLabel(391117, "未命名")}
                                        </RadioItem>
                                    ))}
                                </List>
                            }
                        </div>
                        <div>
                            {
                                toJS(selectData).beagenters.length > 0 && <List renderHeader={() => getLabel(84523, "代理他人创建")}>
                                    {toJS(selectData).beagenters.map((d) => (
                                        <RadioItem
                                            key={d.id}
                                            checked={idSF === d.id}
                                            onClick={() => {
                                                this.handleOnChange(d, "beangener")
                                            }}
                                        >
                                            {d.lastname}（{d.subcompanyName || getLabel(391117, "未命名")}/{d.departmentName || getLabel(391117, "未命名")}/{d.jobtitlename || getLabel(391117, "未命名")}）
                                    </RadioItem>
                                    ))}
                                </List>
                            }

                        </div>
                    </div>
                    <div className="wf-add-bottom-button-part" >
                        <ButtonGroup
                            data={[{
                                key: 'save',
                                text: getLabel(826, "确定"),
                                disabled: disabled,
                                style: { background: disabled ? "#bfbfbf" : "#55B1F9 ", color: "#ffffff" },
                                onClick: () => {
                                    const { resultDatas, setState, setSS, queryParams, parentURL, openByDefaultBrowser } = this.props.wfworkflowAddStore;
                                    let urlParams = { ...resultDatas };
                                    if(openByDefaultBrowser==1){
                                        urlParams.openByDefaultBrowser=openByDefaultBrowser;
                                    }
                                    if (queryParams.hasOwnProperty("src") && queryParams.src === "meeting") {
                                        //检测到设备是ios的时候才编码
                                        if ((/OS [\d._]+/gi).test(window.navigator.userAgent)) {
                                            for (let key in queryParams) {
                                                if (key.includes("field")) {
                                                    queryParams[key] = encodeURIComponent(queryParams[key])
                                                }
                                            }
                                        }
                                        urlParams = { ...urlParams, ...queryParams }
                                    }
                                    setSS();
                                    let returnUrl = parentURL;
                                    if (returnUrl.indexOf("/spa") > -1) {
                                        returnUrl = returnUrl.substr(returnUrl.indexOf("/spa"));
                                    }
                                    forwardViewForm(urlParams, () => { }, "", returnUrl);
                                    let obj = {//带到form表单中的数据
                                        iscreate: '',//新建进入
                                        workflowid: '',//流程id
                                        isagent: '0',//是否是代理
                                        f_weaver_belongto_userid: '',//
                                        beagenter: '0',//代理人
                                        f_weaver_belongto_usertype: '0',
                                    }
                                    setState({ resultDatas: obj, idSF: 0 })
                                    setTimeout(()=>{
                                        this.props.history.go(-1);
                                    },1500)
                                },
                            }]}
                        />
                    </div>
                </div>
            )
        } else return null;
    }
}